import React, {useState,useEffect} from 'react';
import { Form, Button ,DatePicker,Modal,Row,Col,message,Upload,Input} from 'antd';
const FormItem = Form.Item;
import { UploadOutlined } from '@ant-design/icons';
import {getUploadUrl} from "@/utils/upload";
import moment from 'moment';

const formLayout = {
  labelCol: { span: 7 },
};

const colStyle = {
  xl: 18,
  lg: 18,
  md: 18,
  sm: 18,
  xs: 18,
};

const OfflineAddForm = (props) => {
  const { onSubmit, onCancel,record, loading,visible,type} = props;
  const [form] = Form.useForm();
  const [fileName, setFileName] = useState(null);
  const [fileUploadUrls, setFileUploadUrls] = useState([]);

  const fileList = {
    name: 'file',
    multiple: true,
    action: getUploadUrl(),
    data: {name: fileName,catlog:'contract'},
    beforeUpload(file) {
      setFileName(file.name)
    },
    onChange(res) {
      const {status} = res.file;
      if (status === 'done') {
        if (res.file.response.error) {
          message.success(`${res.file.response.error}`);
        } else {
          setFileUploadUrls([...fileUploadUrls, res.file.response]);
          message.success(`${res.file.name} 文件上传成功`);
        }
      } else if (status === 'error') {
        message.error(`${res.file.name} 文件上传失败`);
      }
    },
    onRemove(info) {
      let urls = fileUploadUrls;
      let index = urls.indexOf(info.url);
      if (index > -1) {
        urls.splice(index, 1);
        setFileUploadUrls(urls);
      }
    },
  };

  useEffect(() => {
    if(visible){
      form.setFieldsValue({name:record.name,createTime:type === 'relieve' ? moment(record.endTime) : moment(record.dimissionTime)});
    }
  },[visible]);

  const onFormCancel = () => {
    form.resetFields();
    onCancel();
    setFileUploadUrls([]);
  };

  const handleSubmit = async () => {
    const fieldsValue = await form.validateFields();
    form.resetFields();
    onSubmit({
      ...fieldsValue,
      id:record.id,
      createTime:moment(fieldsValue.createTime).format('YYYY-MM-DD hh:mm:ss'),
      downloadDocUrl:fileUploadUrls.join(',')
    });
    setFileUploadUrls([]);
  };


  const renderContent = () => {
    return (
      <>
        <Row gutter={24} justify='center'>
          <Col {...colStyle}>
            <FormItem name="name" label="签订人">
              <Input disabled={true}/>
            </FormItem>
          </Col>
          <Col {...colStyle}>
            <FormItem name="createTime" label="签订时间">
              <DatePicker disabled={true} placeholder="请选择签订时间" className='widthStyle'/>
            </FormItem>
          </Col>
          <Col {...colStyle}>
            <FormItem name="fileUrl" label="合同文件" rules={[{required: true, message: '合同文件不能为空'}]}>
              <Upload {...fileList}>
                <Button>
                  <UploadOutlined/> 点击上传附件
                </Button>
              </Upload>
            </FormItem>
          </Col>
        </Row>
      </>
    )
  };

  const renderFooter = () => {
    return (
      <>
        <Button onClick={() => onFormCancel()}>取消</Button>
        <Button type="primary" onClick={() => handleSubmit()} loading={loading}>提交</Button>
      </>
    )
  };

  return (
    <Modal
      width={'40%'}
      destroyOnClose
      centered
      title={type === 'relieve' ? '解除合同协议上传':'离职证明上传'}
      visible={visible}
      footer={renderFooter()}
      onOk={handleSubmit}
      onCancel={() => onFormCancel()}
      onClose={() => onFormCancel()}
      okText="确认"
      cancelText="取消"
    >
      <Form
        {...formLayout}
        form={form}
        style={{ paddingTop: '24px' }}
      >
        {renderContent()}
      </Form>
    </Modal>
  )
};

export default (OfflineAddForm);
